﻿using Inovout.Kuajing.Models;
using Inovout.Kuajing.Services;
using Inovout.Memberships;
using Inovout.Memberships.Models;
using Inovout.Memberships.Services;
using Inovout.Security;
using Inovout.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.Routing;

namespace Inovout.Kuajing.Store.Web.Controllers
{
    public class KJBaseController<T> : StoreBaseController<T> where T : class
    {
        public KJBaseController()
        {

        }

        protected override IMembership Membership
        {
            get { return MembershipUser.Membership; }
        }
        protected override ISystemUser SystemUser
        {
            get { return MembershipUser.SystemUser; }
        }

        protected Consigner Consigner
        {
            get;
            set;
        }
        protected ClaimedIdentifier ClaimedIdentifier { get; set; }

        protected IMembershipUser MembershipUser { get; set; }

        protected override void Initialize()
        {
            if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                MembershipUser = ComponentRegistry.Resolve<IMembershipUserService>().FindByClaimedIdentifier(Thread.CurrentPrincipal.Identity.Name);
                ClaimedIdentifier = ClaimedIdentifier.Parse(Thread.CurrentPrincipal.Identity.Name);
                ViewBag.MembershipName = Thread.CurrentPrincipal.Identity.Name;


                if (MembershipUser == null)
                {

                    MembershipUserService membershipUserService = ComponentRegistry.Resolve<MembershipUserService>();
                    SystemUser systemUser = new SystemUser();
                    systemUser.Name = System.Threading.Thread.CurrentPrincipal.Identity.Name;
                    var membershipUser = new MembershipUser(
                       new Membership { Name = systemUser.Name, Email = systemUser.Name, Mobile = "" },
                       systemUser,
                       "",
                       MembershipUserStatus.Activate);
                    membershipUser.IdentifierCode = ClaimedIdentifier.Code;
                    membershipUser.Status = Memberships.MembershipUserStatus.Activate;
                    membershipUserService.Save(membershipUser as Inovout.Memberships.Models.MembershipUser);



                }
                else
                {
                    //Membership membersip = (Membership)MembershipUser.Membership;
                    ConsignerService consignerService = ComponentRegistry.Resolve<ConsignerService>();
                    Consigner = consignerService.FindConsignerByLoginName(System.Threading.Thread.CurrentPrincipal.Identity.Name);
                    if (Consigner != null)
                    {
                        ViewBag.ConsignerId = Consigner.Id;
                    }
                }
            }
        }
    }
}